package com.flying.user.security.jwt;

import com.flying.user.model.Authority;
import com.flying.user.model.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

public final class JwtUserDetailsFactory {

    private JwtUserDetailsFactory() {
    }

    public static JwtUserDetails create(User user) {
        return new JwtUserDetails(user.getName(), user.getPassword(),
                mapToGrantedAuthorities(user.getAuthorities()), user.getEnabled());
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<Authority> authorities) {
        return authorities.stream()
                .map(authority -> new SimpleGrantedAuthority(authority.getName().name()))
                .collect(Collectors.toList());
    }
}